# Open PowerShell/VSCode/PowerShell ISE as Admin

# The server names here are from the demo in the session. 
# You need to change them accordingly, otherwise they'll fail


# Let's install dbatools
Install-Module dbatools -Force

# That was easy!!

# So what do we have to choose from?
Get-Command -Module dbatools

# That was too much!
# How about we reduce that list a bit
Get-Command -Module dbatools -Name *job*

#Or even do a more detailed search of just the dbatools commands
Find-DbaCommand *job*


# We are going to be working with sql users for this demo
# lets safely collect credentials for reuse
# if you are using Windows Auth, then skip this and remove "-SqlCredential $cred" from each command below
$cred = Get-Credential


#Set $sqlinstance1 to a server that you want to try this out on
$sqlinstance1 = "localhost:50120"

# These are for a second server and multi-instance looping
$sqlinstance2 = "localhost:50121"
$sqlinstances = $sqlinstance1,$sqlinstance2

# Lets list a job on a SQL Server instance
Find-DbaAgentJob -SqlInstance $sqlinstance1 -SqlCredential $cred -Name *Job*

#Lets list all backup jobs
Find-DbaAgentJob -SqlInstance $sqlinstance1 -SqlCredential $cred -Name *backup*

#That sucks to read.....
Find-DbaAgentJob -SqlInstance $sqlinstance1 -SqlCredential $cred -Name *backup* | Out-GridView

# Now lets look at best practices.....
Test-DbaMaxDop -SqlInstance $sqlinstance1 -SqlCredential $cred

# Test MaxDOP on multiple servers now
Test-DbaMaxDop -SqlInstance $sqlinstances -SqlCredential $cred | Out-GridView

# Are our servers on the latest build?
Test-DbaBuild -SqlInstance $sqlinstances -SqlCredential $cred -Latest | Out-GridView

# Who are the database owners?
Test-DbaDbOwner -SqlInstance $sqlinstances -SqlCredential $cred | Out-GridView

# Let's fix "broken" owners -WhatIf shows us what would happen if we ran the command. 
# Remove "-WhatIf" to really run it
Set-DbaDbOwner -SqlInstance $sqlinstances -SqlCredential $cred -WhatIf

# Test the power plan of a computer/server
Test-DbaPowerPlan -ComputerName xps13-william

# Set the power plan to high performance
Set-DbaPowerPlan -ComputerName xps13-william -PowerPlan 'High Performance'

# Now let's do a whole migration!
# We can exclude and include as much as we want/need
Start-DbaMigration -Source $sqlinstance1 -Destination $sqlinstance2 -SourceSqlCredential $cred -DestinationSqlCredential $cred -BackupRestore -SharedPath \\unc\NetworkShare\ -Exclude LinkedServers, Credentials, CentralManagementServer, BackupDevices